<template>
    <div class="list">
        <ul class="item" v-for='item of letter' :key="item" :ref="item"
        @click='handleLetterClick'
        @touchstart='handleTouchStart'
        @touchmove='handleTouchMove'
        @touchend='handleTouchEnd'
        >{{item}}</ul>
    </div>
</template>
<script>
export default {
  name: 'Cityalphabet',
  props: {
    cities: Object
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  updated () { // 更新后且渲染后执行
    this.startY = this.$refs['A'][0].offsetTop
  },
  computed: {
    letter () {
      const letter = []
      for (let i in this.cities) {
        letter.push(i)
      }
      return letter
    }
  },
  methods: {
    handleLetterClick (e) {
      this.$emit('change', e.target.innerText)
    //   console.log(e.target.innerText)
    },
    handleTouchStart () {
      this.touchStatus = true
    },
    handleTouchMove (e) {
      if (this.touchStatus) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 79
          const index = Math.floor((touchY - this.startY) / 20)
          if (index >= 0 && index < this.letter.length) {
            this.$emit('change', this.letter[index])
          }
        }, 16)
        // console.log(index)
      }
    },
    handleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>
<style lang="stylus" scoped>
    @import '../../../assets/style/varibles.styl'
    .list
        position: absolute
        top: 1.5rem
        bottom: 0
        right: 0
        display: flex
        flex-direction: column
        justify-content:center
        .item
            text-algin: center
            color: $bgColor
            line-height: .4rem
</style>
